package com.qj.daibei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qj.daibei.pojo.DataFiling;
import com.qj.daibei.pojo.User;
import com.qj.daibei.service.DataFilingService;
import com.qj.daibei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * author zyc
 * Created by sugar zhang on 2021/9/22 11:45
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;

    // wml
    @Autowired
    private DataFilingService dataFilingService;

    @RequestMapping("/register")
    public String register(User user){
        System.out.println(user);
        // wml -- start
        boolean flag = false;
        user.setSuccReqNum(0);
        user.setOverdueNum(0);
        user.setRewardPoints(0);
        user.setCreditPoints(0);
        user.setPinMoney(0f);
        user.setFillIn("0");
        // wml -- end

        flag = service.save(user);

        // wml -- start
        if (flag) {
            DataFiling dataFiling = new DataFiling();
            dataFiling.setUserId(service.login(user.getUserName(), user.getPassword()).getUserId());
            dataFiling.setResult(0);
            dataFiling.setFillIn("0");
            this.dataFilingService.saveDataFiling(dataFiling);
        }
        // wml -- end

        return "login";
    }

    @RequestMapping("/findtel")
    @ResponseBody
    public List<User> findtel(){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.select("tel","user_name");
        List<User> list = service.list(wrapper);
        System.out.println(list);
        return list;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Integer login(String userName, String password, HttpSession session){
        User user = service.login(userName, password);
        int result = 0;
        if (user != null) {
            session.setAttribute("loginUser", user);
            result = 1;
        }
        return result;
    }
}
